package com.ruoyi.module.controller;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.module.domain.UserEvalRecord;
import com.ruoyi.module.service.IUserEvalRecordService;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 用户评测记录Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/eval")
public class UserEvalRecordController extends BaseController
{
    @Autowired
    private IUserEvalRecordService userEvalRecordService;

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查询用户评测记录列表
     */
    @GetMapping("/list")
    public AjaxResult list(UserEvalRecord userEvalRecord)
    {
        userEvalRecord.setUserId(SecurityUtils.getUserId());
        List<UserEvalRecord> list = userEvalRecordService.selectUserEvalRecordList(userEvalRecord);
        return AjaxResult.success(list);
    }

    /**
     * 获取用户评测记录详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userEvalRecordService.selectUserEvalRecordById(id));
    }

    /**
     * 根据UUID查询评测记录
     */
    @GetMapping("/uuid/{uuid}")
    public AjaxResult getByUuid(@PathVariable("uuid") String uuid)
    {
        List<UserEvalRecord> records = userEvalRecordService.selectUserEvalRecordByUuid(uuid);
        return AjaxResult.success(records);
    }

    /**
     * 新增用户评测记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody UserEvalRecord userEvalRecord)
    {
        userEvalRecord.setUserId(SecurityUtils.getUserId());
        return toAjax(userEvalRecordService.insertUserEvalRecord(userEvalRecord));
    }

    /**
     * 批量新增用户评测记录
     */
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody BatchEvalRecordRequest request)
    {
        try {
            Long userId = SecurityUtils.getUserId();
            List<UserEvalRecord> records = new ArrayList<>();
            
            for (EvalWordResult wordResult : request.getWords()) {
                UserEvalRecord record = new UserEvalRecord();
                record.setUserId(userId);
                record.setDictId(request.getEvalId());
                record.setChapter(request.getEvalChapter());
                record.setCategory(request.getCategory());
                record.setTimestamp(System.currentTimeMillis());
                record.setWord(wordResult.getWord());
                record.setScore(wordResult.getScore());
                record.setDuration(wordResult.getDuration());
                record.setUuid(request.getUuid());
                
                // 将lines数据序列化为JSON
                if (wordResult.getLines() != null) {
                    record.setEvalDetail(objectMapper.writeValueAsString(wordResult.getLines()));
                }
                
                records.add(record);
            }
            
            int result = userEvalRecordService.batchInsertUserEvalRecord(records);
            return toAjax(result);
        } catch (Exception e) {
            logger.error("批量保存评测记录失败", e);
            return AjaxResult.error("保存失败: " + e.getMessage());
        }
    }

    /**
     * 修改用户评测记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody UserEvalRecord userEvalRecord)
    {
        return toAjax(userEvalRecordService.updateUserEvalRecord(userEvalRecord));
    }

    /**
     * 删除用户评测记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userEvalRecordService.deleteUserEvalRecordByIds(ids));
    }

    /**
     * 批量评测记录请求对象
     */
    public static class BatchEvalRecordRequest {
        private String category;
        private String evalId;
        private Integer evalChapter;
        private String uuid;
        private List<EvalWordResult> words;

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public String getEvalId() {
            return evalId;
        }

        public void setEvalId(String evalId) {
            this.evalId = evalId;
        }

        public Integer getEvalChapter() {
            return evalChapter;
        }

        public void setEvalChapter(Integer evalChapter) {
            this.evalChapter = evalChapter;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public List<EvalWordResult> getWords() {
            return words;
        }

        public void setWords(List<EvalWordResult> words) {
            this.words = words;
        }
    }

    /**
     * 评测单词结果对象
     */
    public static class EvalWordResult {
        private Integer score;
        private Integer duration;
        private String word;
        private Object lines;

        public Integer getScore() {
            return score;
        }

        public void setScore(Integer score) {
            this.score = score;
        }

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public Object getLines() {
            return lines;
        }

        public void setLines(Object lines) {
            this.lines = lines;
        }
    }
}

